from django.contrib import admin
from django.db import models

# Create your models here.
from django.db import models
from django.utils import timezone
import datetime

class Question(models.Model):
    question_text = models.CharField("问题描述",max_length=200)
    pub_date = models.DateTimeField('发布时间')

    @admin.display(
        boolean=True,
        ordering='pub_date',
        description='是否最近发布',)
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1) and self.pub_date<=timezone.now()

    def __str__(self):
        return self.question_text

    class Meta:
        db_table="question"

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField("选项描述",max_length=200)
    votes = models.IntegerField("票数",default=0)

    def __str__(self):
        return self.choice_text

    class Meta:
        db_table="choice"

class User(models.Model):
    uid=models.CharField("账号",primary_key=True,max_length=255)
    password=models.CharField("密码",max_length=255)
    choice=models.ManyToManyField(to=Choice)


    class Meta:
        db_table="user"

